/*jslint sloppy: true */
/*global require */
var angular = require('angular');
var LM = require('../global');
var RP = require('lemonce-result-parser');
require('./overview.html');
require('./style.less');

LM.controller('Overview', function ($scope, $routeParams, $location,
									 Project, Result, Task, PublicTask, PublicResult) {
	var projectId = $routeParams.projectId,
		taskId = $routeParams.taskId,
		isPublic = $routeParams.public,
		isFromPublic = !!$scope.navConfig.isFromPublic;

	$scope.task = (isPublic ? PublicTask : Task).get({
		taskId: taskId,
		projectId: projectId
	}, function () {
		var result;
		$scope.task.schemas = angular.fromJson($scope.task.schemas);
		result = (isPublic ? PublicResult : Result).query({ projectId: projectId, task: taskId }, function () {
			var resOpts = { name: 's13', tR: [] };
			
			angular.forEach(result, function (r) {
				resOpts.tR.push({
					data: angular.fromJson(r.data)
				});
			});
			$scope.result = new RP($scope.task, resOpts);
			$scope.overview = $scope.result.getOverview();
		});
	});
	$scope.jumpto = function (logIndex, caseIndex) {
		$location.path($scope.baseUrl + '/case/' + caseIndex).hash('log' + logIndex);
	};

	// init
	$scope.project = Project.get({ projectId: projectId });
	$scope.baseUrl = '/project/' + projectId + '/task/' + taskId + '/result';

	$scope.queryConfig.clear().setActive();
	$scope.navConfig.setHref('#/project/' + projectId + '/task/' +
		(isFromPublic ? 'public' : taskId)).setType('back').isFromPublic = false;
});
